package tech.aiflowy.auth.controller;

import cn.dev33.satoken.exception.NotLoginException;
import org.apache.commons.lang.StringUtils;
import tech.aiflowy.auth.entity.LoginDTO;
import tech.aiflowy.auth.entity.LoginVO;
import tech.aiflowy.auth.service.AuthService;
import tech.aiflowy.common.domain.Result;
import tech.aiflowy.common.util.StringUtil;
import tech.aiflowy.common.web.jsonbody.JsonBody;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.aiflowy.system.entity.SysToken;
import tech.aiflowy.system.service.SysTokenService;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/auth/")
public class AuthController {

    @Resource
    private AuthService authService;
    @Resource
    private SysTokenService tokenService;

    @PostMapping("login")
    public Result login(@JsonBody LoginDTO loginDTO) {
        LoginVO res = authService.login(loginDTO);
        return Result.success(res);
    }

  /*  @PostMapping("loginToken")
    public Result login() {
        String token =StpUtil.getTokenValue();

        if(StringUtils.isBlank(token)){
            throw new NotLoginException("token为空","login","login");
        }
        SysToken res = tokenService.getByToken(token);
        if(res==null){
            throw new NotLoginException("token错误","login","login");
        }

        if(!token.equals(res.getTokenValue())){
            throw new NotLoginException("token不正确","login","login");
        }

        LoginVO loginVO = authService.login(res.getCreatedBy(),token);

        return Result.success(loginVO);
    }*/

    @PostMapping("logout")
    public Result logout() {
        StpUtil.logout();
        return Result.success();
    }
}
